/**
 * 
 */
package evolution;

import java.util.ArrayList;

import ca.swank.hardquor.Move;

/**
 * @author alonseg
 *
 */
public class ReservationTreeNode {

	private Move move;
	private double value;
	private ArrayList<ReservationTreeNode> children;
	private ReservationTreeNode parent;
	
	/**
	 * 
	 */
	public ReservationTreeNode(Move m, double v) {
		this.move = m;
		this.value = v;
		this.parent = null;
		children = new ArrayList<ReservationTreeNode>();
	}
	
	public ReservationTreeNode(Move m, double v, ReservationTreeNode parent) {
		this(m,v);
		this.parent = parent;
	}

	public Move getMove() {
		return move;
	}

	public void setMove(Move move) {
		this.move = move;
	}

	public double getValue() {
		return value;
	}

	public void setValue(double value) {
		this.value = value;
	}
	
	public void addChild(ReservationTreeNode rtn) {
		this.children.add(rtn);
	}
	
	public boolean isLeaf() {
		return this.children.isEmpty();
	}

	public ArrayList<ReservationTreeNode> getChildren() {
		return this.children;
	}
	
	public ReservationTreeNode getParent() {
		return this.parent;
	}
	
	public ReservationTreeNode findMinMax(int level) {
		if (this.isLeaf()) return null;
		ReservationTreeNode res = children.get(0);
		if(level % 2 == 0) {
			for(int i = 1; i < this.children.size(); i++) {
				if(res.getValue() < children.get(i).getValue())
					res = children.get(i);
			}
		} else {
			for(int i = 1; i < this.children.size(); i++) {
				if(res.getValue() > children.get(i).getValue())
					res = children.get(i);
			}
		}
		
		return res;
	}
}
